package com.example.demo.service.impl;

import com.example.demo.service.ApiSoftService;
import com.example.demo.service.model.ApiPageRequest;
import com.example.demo.service.model.ApiSoftRecords;
import com.example.demo.service.model.ApiSoftRequest;
import com.example.demo.service.model.ApiSofResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

@Slf4j
public class ApiSoftServiceImpl implements ApiSoftService {
    @Override
    @Transactional
    public ApiSoftRecords querySoft(ApiSoftRequest apiSoftRequest) {
        return ApiSoftRecords.builder()
                .softName(apiSoftRequest.getSoftName())
                .softVersion(apiSoftRequest.getSoftVersion())
                .gitUrl(apiSoftRequest.getGitUrl())
                .mainLanguage(apiSoftRequest.getMainLanguage())
                .languages(apiSoftRequest.getLanguages())
                .build();
    }

    public ApiSofResponse queryFuzzyPageInvoke(ApiPageRequest apiPageRequest) {
        log.info("queryFuzzyPageInvoke");
        log.info(apiPageRequest.getSoftName());
        log.info(apiPageRequest.getPage().toString());
        log.info(apiPageRequest.getSize().toString());

        String url = "http://localhost:8181/soft/pageDef";
        RestTemplate restTemplate = new RestTemplateBuilder()
                .defaultHeader("Authorization", "545646")
                .build();
        ApiSofResponse apiSofResponse;
        try {
            apiSofResponse = restTemplate.getForObject(url, ApiSofResponse.class);
        } catch (Exception e) {
            log.error("queryFuzzyPageInvoke error", e);
            throw new RuntimeException("调用错误");
        }
        return apiSofResponse;
    }

}
